package com.xvyy.tingshu.user.service;


import com.alibaba.fastjson.JSONObject;
import com.xvyy.tingshu.user.strategy.ProcessDefaultPayRecord;
import com.xvyy.tingshu.vo.user.UserPaidRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * ClassName: MqOpsServiceImpl
 * Package: com.xvyy.tingshu.user.service
 *
 * @Description:
 * @Author: xvyy
 * @Create: 2025/2/13 - 14:54
 * @Version: v1.0
 */
@Service
@Slf4j
public class MqOpsServiceImpl implements MqOpsService {

    @Autowired
    private Map<String, ProcessDefaultPayRecord> stringProcessDefaultPayRecordMap;

    @Override
    public void updatePayRecord(String content) {
        UserPaidRecordVo userPaidRecordVo = JSONObject.parseObject(content, UserPaidRecordVo.class);
        String itemType = userPaidRecordVo.getItemType();
        ProcessDefaultPayRecord processDefaultPayRecord = stringProcessDefaultPayRecordMap.get(itemType);
        processDefaultPayRecord.processPayRecord(userPaidRecordVo);
        /*        String itemType = "";
        switch (itemType) {
            case "1001" -> dealAlbumItemType(userPaidRecordVo);// 处理专辑类型
            case "1002" -> dealTrackItemType(userPaidRecordVo);// 处理声音类型
            case "1003" -> dealVipItemType(userPaidRecordVo);// 处理vip类型
        }*/
    }


    // 基础写法：
    /* *//**
     * 处理专辑类型
     *//*
    private void dealAlbumItemType(UserPaidRecordVo userPaidRecordVo) {
        String orderNo = userPaidRecordVo.getOrderNo();
        UserPaidAlbum userPaidAlbum = userPaidAlbumMapper.selectOne(new LambdaQueryWrapper<UserPaidAlbum>().eq(UserPaidAlbum::getOrderNo, orderNo));
        if (userPaidAlbum != null) {
            throw new TsException(201, "该专辑类型的订单已经处理过，请不要重复处理");
        }
        userPaidAlbum.setOrderNo(orderNo);// 设置订单号
        userPaidAlbum.setUserId(userPaidRecordVo.getUserId());// 设置用户id
        userPaidAlbum.setAlbumId(userPaidRecordVo.getItemIdList().get(0));// 设置专辑id
        int count = userPaidAlbumMapper.insert(userPaidAlbum);
        log.info("保存订单号为{}的专辑类型{}，数据记录到数据库影响行数为{}", orderNo, count > 0 ? "成功" : "失败", count);
    }

    *//**
     * 处理声音类型
     *//*
    private void dealTrackItemType(UserPaidRecordVo userPaidRecordVo) {
        String orderNo = userPaidRecordVo.getOrderNo();
        UserPaidTrack userPaidTrack = userPaidTrackMapper.selectOne(new LambdaQueryWrapper<UserPaidTrack>().eq(UserPaidTrack::getOrderNo, orderNo));
        if (userPaidTrack != null) {
            throw new TsException(201, "该声音类型的订单已经处理过，请不要重复处理");
        }
        // 远程调用album微服务获取声音详情信息
        Result<List<TrackListVo>> trackListVoResult = albumInfoFeignClient.getTrackListVo(userPaidRecordVo.getItemIdList());
        List<TrackListVo> trackListVoListData = trackListVoResult.getData();
        Assert.notNull(trackListVoListData, "远程调用album微服务获取声音详情信息失败");
        AtomicInteger count = new AtomicInteger();
        userPaidRecordVo.getItemIdList().forEach(trackId -> {
            userPaidTrack.setOrderNo(orderNo);
            userPaidTrack.setUserId(userPaidRecordVo.getUserId());
            userPaidTrack.setTrackId(trackId);
            count.set(userPaidTrackMapper.insert(userPaidTrack));
        });
        log.info("保存订单号为{}的声音类型{}，数据记录到数据库影响行数为{}", orderNo, count.get() > 0 ? "成功" : "失败", count);
    }

    *//**
     * 处理vip类型
     *//*
    private void dealVipItemType(UserPaidRecordVo userPaidRecordVo) {
        String orderNo = userPaidRecordVo.getOrderNo();
        // 1. 查询数据库中是否存在该订单号的vip服务记录
        UserVipService userVipService = userVipServiceMapper.selectOne(new LambdaQueryWrapper<UserVipService>().eq(UserVipService::getOrderNo, orderNo));
        if (userVipService != null) {
            throw new TsException(201, "该vip类型的订单已经处理过，请不要重复处理");
        }
        // 2. 根据订单号获取vip套餐信息
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(userPaidRecordVo.getItemIdList().get(0));
        if (vipServiceConfig == null) {
            throw new TsException(201, "根据订单号获取vip套餐信息失败");
        }
        Integer serviceMonth = vipServiceConfig.getServiceMonth();// 获取服务时长（月）
        // 3. 根据用户id获取用户信息
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        // 4. 获取用户的vip身份
        Integer isVip = userInfo.getIsVip();
        // 5. 获取vip过期时间
        Date vipExpireTime = userInfo.getVipExpireTime();
        // 6. 获取日期对象
        Calendar calendar = Calendar.getInstance();
        // 7. 判断用户是否已经是vip
        if (isVip == 0) {
            // 7.1 不是vip
            calendar.setTime(new Date());
        } else {
            // 7.2 是vip
            if (vipExpireTime.before(new Date())) {
                // 7.2.1 是vip 过期了
                calendar.setTime(new Date());
            } else {
                // 7.2.2 是vip 没过期
                calendar.setTime(vipExpireTime);
            }
        }
        // 7.3 计算vip过期时间
        calendar.add(Calendar.MONTH, serviceMonth);
        // 8. 记录vip服务记录
        userVipService.setOrderNo(orderNo);
        userVipService.setUserId(userPaidRecordVo.getUserId());
        userVipService.setStartTime(new Date());// 设置开始时间
        int vipLogCount = userVipServiceMapper.insert(userVipService);
        log.info("保存订单号为{}的vip类型{}，数据记录到数据库影响行数为{}", orderNo, vipLogCount > 0 ? "成功" : "失败", vipLogCount);
        // 9. 更新用户信息
        userInfo.setIsVip(1);// 设置为vip
        userInfo.setVipExpireTime(userVipService.getExpireTime());// 设置vip过期时间
        int vipUpdateCount = userInfoMapper.updateById(userInfo);
        log.info("更新用户信息{}，数据记录到数据库影响行数为{}", vipUpdateCount > 0 ? "成功" : "失败", vipUpdateCount);
    }*/
}


